package com.wxc.api.admin.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.wxc.api.admin.entity.SysMenu;
import com.wxc.api.admin.mapper.SysMenuMapper;
import com.wxc.api.admin.service.ISysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2022-04-28
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

    @Autowired
    private SysMenuMapper menuMapper;

    @Override
    public List<SysMenu> queryCatList() {
        Object userId = StpUtil.getLoginId();
        List<String> permissionList = StpUtil.getPermissionList(StpUtil.getLoginId());
        List<SysMenu> data = menuMapper.selectList(null);
        List<SysMenu> categoriesList = new ArrayList<>();

        for (SysMenu category : data) {
            if(userId.equals("1") || Objects.equals(category.getPermission(), "") || permissionList.contains(category.getPermission())){
                if (category.getPid() == 0) {
                    categoriesList.add(category);
                }
            }
        }

        for (SysMenu category : categoriesList) {
            category.setTreeList(getChilde(category.getId(), data));
        }
        return categoriesList;
    }

    private List<SysMenu> getChilde(Integer id, List<SysMenu> rootList) {
        List<String> permissionList = StpUtil.getPermissionList(StpUtil.getLoginId());
        Object userId = StpUtil.getLoginId();
        List<SysMenu> childList = new ArrayList<>();
        for (SysMenu category : rootList) {
            if (category.getPid().equals(id)) {
                if(userId.equals("1") || Objects.equals(category.getPermission(), "") || permissionList.contains(category.getPermission())){
                    childList.add(category);
                }
            }
        }
        for (SysMenu category : childList) {
            category.setTreeList(getChilde(category.getId(), rootList));
        }
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }

    @Override
    public Integer add(String name, String permission, String path, Integer pid, Boolean is_show) {
        if (pid > 0 && menuMapper.selectById(pid) == null) {
            return 0;
        }

        SysMenu sysMenu = new SysMenu();
        sysMenu.setName(name);
        sysMenu.setPermission(permission);
        sysMenu.setPath(path);
        sysMenu.setPid(pid);
        sysMenu.setIsShow(is_show);

        return menuMapper.insert(sysMenu);
    }

    @Override
    public Integer edit(Integer id, String name, String permission, String path, Integer pid, Boolean is_show) {
        if (pid > 0 && menuMapper.selectById(pid) == null) {
            return 0;
        }

        SysMenu sysMenu = new SysMenu();
        sysMenu.setId(id);
        sysMenu.setName(name);
        sysMenu.setPermission(permission);
        sysMenu.setPath(path);
        sysMenu.setPid(pid);
        sysMenu.setIsShow(is_show);

        return menuMapper.updateById(sysMenu);
    }

    @Override
    public List<SysMenu> getInfo(List<Integer> ids) {
        return menuMapper.selectBatchIds(ids);
    }

}
